import {
  _path,
  Write_player,
  existplayer,
  bound,
  Write_cangku,
  _item
} from '../../mode/allapi'
import { plugin, AMessage } from '../../api/api'
export class consume extends plugin {
  constructor() {
    super({
      dsc: '招募',
      rule: [
        {
          reg: /^(#|\/)?募兵.*$/,
          fnc: 'mubin'
        },
        {
          reg: /^(#|\/)?招募.*$/,
          fnc: 'Addbig'
        }
      ]
    })
  }

  async mubin(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const playerPath = await _path(id)
    const player = playerPath.player
    const bingPath = playerPath.bingyin
    const bing1 = await _item(id, 'bing')
    const didian = parseInt(e.msg.replace(/(#|\/)?募兵/, ''), 10)
    if (isNaN(didian)) return false
    const shibin = await bound('士兵', bing1, 'name')
    if (!shibin) return false
    const resources = ['food', 'iron', 'wood']
    const resourceNames = ['粮草', '铁矿', '木材']
    const resourceMultipliers = [20, 10, 10]
    for (let i = 0; i < resources.length; i++) {
      const resource = resources[i]
      const resourceName = resourceNames[i]
      const resourceMultiplier = resourceMultipliers[i]
      if (player[resource + '_num'] < resourceMultiplier * didian) {
        return e.reply(
          `征兵处：主公仓内资源匮缺，还需${
            resourceMultiplier * didian - player[resource + '_num']
          }${resourceName}`
        )
      }
    }

    const existingSoldier = await bound('士兵', bingPath, 'name')
    if (existingSoldier === undefined) {
      bingPath.push({ ...shibin, num: didian })
    } else {
      existingSoldier.num += didian
    }
    Write_cangku('barracks', id, bingPath)
    player.food_num -= 20 * didian
    player.iron_num -= 10 * didian
    player.wood_num -= 10 * didian
    await Write_player(id, player)
    e.reply(
      `征兵处：募兵结束，消耗${20 * didian}粮草,${10 * didian}铁矿，${
        10 * didian
      }木材`
    )
    return true
  }

  async Addbig(e: AMessage) {
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const thing_user = e.msg
    const expecNum = parseInt(thing_user.replace(/(#|\/)?招募/, ''), 10)
    if (isNaN(expecNum)) {
      e.reply('请输入合理的招募数量')
      return true
    }
    let Num: number
    const cangku = await (await _path(id)).cangku
    const player = await (await _path(id)).player
    const found = await bound('普通招募令', cangku, 'name')
    if (found) {
      if (expecNum <= 0) Num = 0
      else if (expecNum > 5) Num = 5
      else Num = expecNum
    } else {
      e.reply('尚书台：主公暂未有普通招募令')
      return true
    }
    Num = Num > found.num ? found.num : Num
    const finalNum = Num
    const log = []
    found.num -= Num
    const bingyin = await (await _path(id)).bingyin
    while (Num > 0) {
      const min = 2001
      const max = 2077
      const randomNum = Math.floor(Math.random() * (max - min + 1)) + min
      const fand = await _item(id, 'bing')
      const wujian = await fand.find(item => item.id == randomNum)
      const sameNameWarriors = player.wujian
        .concat(bingyin)
        .filter(
          warrior =>
            warrior &&
            warrior.name &&
            wujian &&
            wujian.name &&
            warrior.name.startsWith(wujian.name)
        )
      if (sameNameWarriors.length > 0) {
        log.push(
          `尚书台：恭喜主公获得重复武将【${wujian.name}】，已转化为将魂*40\n`
        )
        const good = (await _item(id, 'goods')).find(item => item.id == 50002)
        const found = await bound(good.name, cangku, 'name')
        if (found) {
          found.num += 40
        } else {
          good.num = 40
          cangku.push(good)
        }
      } else {
        log.push(`尚书台：恭喜主公获得猛将【${wujian.name}】\n`)
        bingyin.push(wujian)
        player.militaryNum += 1
      }
      Num--
    }
    await Write_player(id, player)
    await Write_cangku('cangku', id, cangku)
    await Write_cangku('barracks', id, bingyin)
    log.push(`本次招募共计招募${finalNum}次`)
    e.reply(log)
    return true
  }
}
